<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UploadController extends Controller
{
    /**
     * @Notes: 图片上传
     * @Interface:          make
     * @param        Request  $request
     * @return:             array
     * @author:             Jokenzhang
     * @Time:               2020/12/21  17:10
     */
    public function make(Request $request)
    {
        $filepath = $this->move($request->file('file'));
        return [
            'code' => 0,
            'file' => $filepath
        ];
    }

    /**
     * @Notes:  编辑器图片上传
     * @Interface:          simditor
     * @return:             array
     * @author:             Jokenzhang
     * @Time:               2020/12/21  17:17
     */
    public function simditor(Request $request)
    {
        $filepath = $this->move($request->file('file'));
        return [
            'success' => true,
            'msg' => '上传成功',
            'file_path' => $filepath
        ];
    }


    /**
     * @Notes:  图片上传逻辑公共方法
     * @Interface:          move
     * @param $file
     * @return:             \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\UrlGenerator|string
     * @author:             Jokenzhang
     * @Time:               2020/12/21  17:21
     */
    protected function move($file)
    {
        $filename = md5(time()) . "." . $file->getClientOriginalExtension();
        $dir = "uploads/" . date('ym/d');
        $file->move($dir, $filename);
        return url($dir . '/' . $filename);
    }
}
